//
//  SignViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14-8-23.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "SignViewController.h"
#import "Contact.h"
#import "ServerInterface.h"
extern Contact *globalContact;
@interface SignViewController ()<UITextViewDelegate>{

}
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end

@implementation SignViewController
@synthesize resetUserInfoBlock;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"发表" style:UIBarButtonItemStylePlain target:self action:@selector(postSign:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.title = @"工作格言";
    globalContact.mark.length>0?(_textView.text = globalContact.mark):(_textView.text = @"工作格言");
    [_textView becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)postSign:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
    globalContact.mark = _textView.text;
    NSDictionary *   userInfoDic = @{@"user_id":@(globalContact.userID),@"mark":globalContact.mark,@"company_id":CompanyID,@"token":globalContact.token};
    [ServerInterface updateUserInfoWithInfoDic:userInfoDic withBlock:^(NSDictionary *resultDic, NSError *error) {
        if (error) {
        }
        else{
            if ([[resultDic valueForKeyPath:@"success"] integerValue] ==1) {
                BOOL success =[Contact  updateContact:globalContact];
                if (success) {
                }
            }
            else{
                NSString *message =[resultDic objectForKey:@"message"];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
    }];
    
    if (resetUserInfoBlock) {
        self.resetUserInfoBlock();
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    [textView resignFirstResponder];
    
}


- (void)textViewDidChange:(UITextView *)textView{
    _numLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)textView.text.length];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
